package view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.Manuscript;
import model.State;

/**
 * @author Kevin Anderson
 *
 */
@SuppressWarnings("serial")
public class EditPaperFrame extends JFrame{

	private Manuscript my_manuscript;
	private State my_state;
	
	public EditPaperFrame(Manuscript manuscript, State the_state) {
		
		super("Edit Paper");
		
		my_manuscript = manuscript;
		my_state = the_state;
		setResizable(true); 
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
		setLocationRelativeTo(null);
		setVisible(true);

	}

	public void setup() {
		add(new EditPaperPanel());
		pack();

	}
	
	@SuppressWarnings("serial")
	private class EditPaperPanel extends JPanel
	{
		private JTextField titleField;
		private JTextField paperAbstractField;
		private JTextField filePathField;
		
		public EditPaperPanel() {
			super();
			setup();
		}

		public boolean verifyFields()
		{
			
			boolean valid =  (titleField.getText().trim().length() > 0)
					&& (paperAbstractField.getText().trim().length() > 0)
					&& (filePathField.getText().trim().length() > 0);
			 
			if (!valid)
				JOptionPane.showMessageDialog(this, "Not all fields are valid.  Try again");
			
			return valid;
		}
		
		public void setup() {
			GridLayout exp_layout = new GridLayout(0,2);
			
			titleField = new JTextField(my_manuscript.getMy_title());
			paperAbstractField = new JTextField(my_manuscript.getMy_abstract());
			filePathField = new JTextField(my_manuscript.getMy_path());

			this.setLayout(exp_layout);
			this.add(new JLabel("Title"));
			this.add(titleField);
			this.add(new JLabel("Author"));
			this.add(new JLabel(my_state.getUser().getName()));
			this.add(new JLabel("Conference"));
			this.add(new JLabel(my_state.getConference().getConfName()));
			this.add(new JLabel("Abstract"));
			this.add(paperAbstractField);
			this.add(filePathField);
			this.add(new JButton(new BrowseAction()));
			this.add(new JButton(new CancelAction()));
			this.add(new JButton(new SubmitAction()));
		}
		
		
		private class BrowseAction extends AbstractAction
		{
			
			public BrowseAction() {
				super("Browse...");
				putValue(Action.MNEMONIC_KEY, KeyEvent.VK_B);
				putValue(Action.SELECTED_KEY, true);
			}
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser fc = new JFileChooser();
				int returnVal = fc.showOpenDialog(EditPaperPanel.this);

			    if (returnVal == JFileChooser.APPROVE_OPTION) {
			         filePathField.setText(fc.getSelectedFile().getAbsolutePath());
			    }				
			}
		}
		
		private class CancelAction extends AbstractAction
		{
			public CancelAction()
			{
				super("Cancel");
			}

			@Override
			public void actionPerformed(ActionEvent e) {
				EditPaperFrame.this.dispose();				
			}
		}
		
		private class SubmitAction extends AbstractAction
		{
			public SubmitAction()
			{
				super("Submit");
			}

			@Override
			public void actionPerformed(ActionEvent e) {
				if (verifyFields())
				{
					Manuscript updatedPaper = new Manuscript(my_manuscript.getMy_id(),
															 my_manuscript.getMy_author(),
															 my_manuscript.getMy_status(),
															 my_manuscript.getMy_conference(),
															 titleField.getText(),
															 paperAbstractField.getText(),
															 filePathField.getText(),
															 my_manuscript.getMy_spc());
					
					if(my_state.getParser().writeManuscript(updatedPaper))
					{
						JOptionPane.showMessageDialog(EditPaperFrame.this, "You have successfully updated your paper");
						EditPaperFrame.this.dispose();
					} else {
						JOptionPane.showMessageDialog(EditPaperFrame.this, "Sorry, your paper did not save. Try again.");
					}
				}
				
			}
		}
	}
	
	
	
}